<?php
require_once 'common.php';

$user_id = user::getId();
if (empty($user_id))
{
	sys::gotoUrl('/');
	return false;
}

if ($_POST['item_images'])
{
	$posted_item_info = $_POST;
	
	$item_image_name = array_shift($posted_item_info['item_images']);
	$item_image_original = ITEM_IMAGE_UPLOAD_DIR.'/'.$item_image_name;
	if (!file_exists($item_image_original))
	{
		// TODO: should do error flow
		sys::gotoUrl('/');
		return false;
	}
	
	
	$posted_item_info['user_id'] = $user_id;
	$posted_item_info['item_image'] = $item_image_name;

	$item_id = item::add($posted_item_info);
	itemimage::save($item_id, $item_image_original);
	$add_result = $item_id > 0 ? "发表成功！" : "发表失败！";
	
	
	//$_SESSION['action_result'] = $add_result;
	
	sys::gotoUrl('/user/'.$user_id.'/');
	
	/*$item_data = item::getByNameAndType($_POST['item_name'], $_POST['item_type']);
	$item_id = $item_data['item_id'];
	
	if ($item_id && $file['name'])
	{
		sys::hashSave($file['tmp_name'],
			$file['name'],
			ITEM_IMAGE_DIR, $item_id);
	}
	sys::gotoUrl('/');*/
}

$refer_item = item::getById($_GET['item_id']);
$refer_relation_type_id = $_GET['relation_type_id'];
page::printTop();
?>

<script type="text/javascript" charset="utf-8">
$(function(){
	$("#button-confirm-upload").hide();

	var uploader = new qq.FileUploader({
		element:$('#file-uploader')[0],
		params: {},
		allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'bmp'],        
		debug: false,
		onSubmit: function(id, fileName){},
		onProgress: function(id, fileName, loaded, total){},
		onComplete: function(id, fileName, responseJSON){
			if (!responseJSON.success) return false;
			$('#file-list').html('');
			
			$("<input type=hidden name=item_images[] value='"+responseJSON.upname+"'>").appendTo($('#file-list'));
			var im=$("<span>"
				+"<img class=upload_image height=200 src='<?php echo ITEM_IMAGE_UPLOAD_BASE_URL."/"; ?>"+responseJSON.upname+"' /></span>");
			im.appendTo($('#file-list'));
			$(im).bind('click', function(e){
					$(this).remove();
				});
			$('#button-confirm-upload').show();
			
			$('.qq-upload-list').hide();
		},
		onCancel: function(id, fileName){},
		messages: {
		},
		showMessage: function(message){ alert(message); }
	});
});
</script>


<div style="text-align:center">请选择需要上传的图片，写上一段说明文字（注：此项可空），然后就可以发表和大家分享了！<?php echo '<font color=red>'.$add_result.'</font>'; ?></div><hr />

<form name="upform" method=post enctype="multipart/form-data">
	<div style="margin-left:200px;margin-top:50px;">
		<div>
			<div id="file-uploader">
				<noscript>
				<p>Please enable JavaScript to use file uploader.</p>
				<!-- or put a simple form for upload here -->
				</noscript>
			</div>
		</div>
	</div>
	<div id="file-list" style="margin-left:280px;padding-top:10px;"></div>
	<div style="margin-left:200px;">
		<div style="float:left;">添加文字：</div>
		<div>
			<input value="" name="item_emotion" maxlength=200 style="width:400px" />
		<input id="button-confirm-upload" style="width:150px;" type="submit" value=" 发 表！ " />
		</div>
	
	</div>

</form>




<?php
page::printTail();
